const app = getApp();
const req = require("../../utils/http.js");
Page({
  data: {
    product: [],
    merchant: '',
    currentTab: '',
    setShow: false,
    address: '',
    merchants: '',


  },
  //商品详情1
  getProductDetail(e) {
    let t = this
    let params = {}
    let id = e
    let currentTab = t.data.currentTab //'0'(商品)；'1'(优惠卷)
    let url = ''
    let type = t.data.type
    let method=''

    if (currentTab == '0') { //商品
      url = `/coupon/wxProduct/getProductDetail/${id}`
      method='GET'
    } else if (currentTab == '1') { //优惠卷
      url = `/coupon/wxProduct/getCouponDetail/${id}`
      method = 'GET'
    } else if (type) { //新人礼 大转盘
      params = {
        id: t.data.productId,
        type,
      }
      method = 'POST'
      url = `/coupon/wxProduct/getObjectDetail`
      
    }
    req.wxReq(url, method, params).then(res => {
      console.log('商品购买详情',res)
      if (res.data.code == 200) {
        t.setData({
          product: res.data.result,
          merchant: res.data.result.store,
          merchants: res.data.result.stores,
          currentTab,
        })
      }
    })
  },
  // 商品购买
  receiveBuy() {
    let t = this
    let productId = t.data.productId
    let setShow = t.data.setShow
    if (setShow == false) {
      wx.navigateTo({
        url: '../address/index',
      })
    } else {
      wx.showLoading({
        title: '购买中...',
        mask: true,
      })
      let address = t.data.address.data
      let params = {
        productId,
        address
      }
      req.wxReq(`/coupon/wxProduct/wxPay`, "POST", params).then(res => {
        console.log('购买', res)
        wx.hideLoading();
        if (res.data.code == 200) {
          wx.requestPayment({
            timeStamp: res.data.result.timeStamp,
            nonceStr: res.data.result.nonceStr,
            package: res.data.result.packageValue,
            signType: 'MD5',
            paySign: res.data.result.paySign,
            success(res) {
              if (res.errMsg = "requestPayment:ok") {
                wx.switchTab({
                  url: '../index/index',
                })
              }
            },
            fail(res) {
              console.log(res)
            }
          })

        } else if (res.data.code == 7006) {
          let appid = res.data.result.serviceAppId
          let setfrom = res.data.result.from
          let obj = 'wxPay'
          wx.showModal({
            content: res.data.message,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../publicToken/index?productId=' + productId + '&appid=' + appid + '&setfrom=' + setfrom + '&obj=' + obj
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }

          })
        }
      })
    }

  },
  //领取优惠卷
  receiveCoupon(e) {
    let t = this
    let productId = t.data.productId
    let params = {
      productId
    }
    req.wxReq('/coupon/wxProduct/receiveCoupon', "GET", params).then(res => {
      console.log(res)
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1000
        })
        setTimeout(() => {

          wx.navigateBack({
            data: 1
          })
        }, 1300)

      } else if (res.data.code == 7006) { //未授权
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1500
        })
        let appid = res.data.result.serviceAppId
        let setfrom = res.data.result.from
        let obj = 'receiveCoupon'
        setTimeout(() => {
          wx.navigateTo({
            url: '../publicToken/index?productId=' + productId + '&appid=' + appid + '&setfrom=' + setfrom + '&obj=' + obj,
          })
        }, 1600)

      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },

  //查看更多商家
  goMerchantList: function(e) {
    let t = this
    let currentTab = t.data.currentTab
    const productId = this.data.productId;
    if (currentTab == 1) {
      wx.navigateTo({
        url: '../merchantList/index?productId=' + productId
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    let t = this
    wx.getStorage({
      key: 'address',
      success: function(res) {
        if (res) {
          t.setData({
            address: res,
            setShow: true
          })

        } else {
          console.log('没有信息')
        }
      },
    })
  },
  onLoad: function(options) {
    console.log(options)
    let t = this
    let currentTab = options.currentTab
    let productId = options.productId
    let setType = options.setType //新人礼
    let type = options.type
    this.setData({
      productId: options.productId,
      currentTab,
      type,
      setType
    });
    t.getProductDetail(productId)
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('appName'),
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})